from tkinter import *
from tkinter.filedialog import askdirectory, askopenfilename
from caterpillar_common.common.excel import Excel


class MainWindow(object):
    def __init__(self):
        self.__window = Tk()
        self.__version = "0.0.1"
        self.__init_window()

    def __get_screen_width_and_length(self):
        return self.__window.winfo_screenwidth(), self.__window.winfo_screenheight()

    def __select_dir(self):
        cur_dir = askdirectory()
        self.__cur_dir.set(cur_dir)

    def __select_file(self):
        cur_file = askopenfilename()
        self.__cur_file.set(cur_file)

    def __gen_pytest_script_file(self, data):
        pass

    def __gen_pytest_script(self):
        sheet = self.__entry3.get()
        excel = Excel(self.__entry1.get())
        self.__text.insert("insert", "开始读取excel数据......\n")
        all_datas = excel.get_rows(sheet, is_dict=True)
        if len(all_datas) > 0:
            self.__text.insert("insert", "已完成读取excel数据：OK\n")
            self.__text.yview_moveto(1)
            self.__text.update()
            self.__text.insert("insert", "开始生成pytest自动化测试脚本文件......\n")
            self.__text.yview_moveto(1)
            self.__text.update()
            for data in all_datas:
                self.__gen_pytest_script_file(data)
        else:
            self.__text.insert("insert", "未读取到数据，请检查sheet名称是否正确：Error\n")
            self.__text.yview_moveto(1)
            self.__text.update()

    def __init_window(self):
        # 设置窗口的标题
        self.__window.title(f"一键生成pytest自动化脚本工具 (v{self.__version})")

        # 获取屏幕的长、宽
        screen_width, screen_height = self.__get_screen_width_and_length()
        # 设置窗口的长、宽
        win_width, win_height = screen_width * 5 // 8, screen_height * 5 // 8
        # 设置窗口在屏幕中居中布局
        self.__window.geometry(
            f"{win_width}x{win_height}+{(screen_width - win_width) // 2}+{(screen_height - win_height) // 2}")
        # 设置窗口长宽是否可变
        self.__window.resizable(width=True, height=True)
        # 设置提示信息
        Label(self.__window, text="测试用例文件:").grid(row=0)
        Label(self.__window, text="脚  本  目  录:").grid(row=1)
        Label(self.__window, text="用 例 sheet 名:").grid(row=2)
        # 存放文件路径的变量
        self.__cur_file = StringVar()
        # 存放目录路径的变量
        self.__cur_dir = StringVar()
        # 设置输入框以及浏览按钮
        self.__entry1 = Entry(self.__window, width=60, textvariable=self.__cur_file)
        self.__entry2 = Entry(self.__window, width=60, textvariable=self.__cur_dir)
        self.__entry3 = Entry(self.__window, width=60)
        self.__entry1.grid(row=0, column=1, sticky=W + E + N + S, padx=5, pady=5)
        self.__entry2.grid(row=1, column=1, sticky=W + E + N + S, padx=5, pady=5)
        self.__entry3.grid(row=2, column=1, sticky=W + E + N + S, padx=5, pady=5)
        Button(self.__window, text='浏览', command=self.__select_file).grid(row=0, column=2, sticky=W, padx=5, pady=5)
        Button(self.__window, text='浏览', command=self.__select_dir).grid(row=1, column=2, sticky=W, padx=5, pady=5)
        # 设置开始生成脚本按钮
        Button(self.__window, text='开始', command=self.__gen_pytest_script).grid(row=3, column=1, sticky=W + E + N + S,
                                                                                padx=5, pady=5)
        # 增加进展信息显示框
        self.__text = Text(self.__window, width=80, height=20)
        self.__text.grid(row=4, column=0, columnspan=3, sticky=W + E + N + S, padx=5, pady=5)
        # 为显示框增加滚动条
        scroll_bar = Scrollbar(orient="vertical", command=self.__text.yview())
        self.__text.config(yscrollcommand=scroll_bar.set)
        scroll_bar['command'] = self.__text.yview
        scroll_bar.grid(row=4, column=3, sticky=S + W + E + N)

    def run(self):
        self.__window.mainloop()


if __name__ == "__main__":
    main_window = MainWindow()
    main_window.run()
